package com.zrf.testchatgpt.shiro.manager;

import com.zrf.testchatgpt.service.impl.RedisServiceImpl;
import lombok.Data;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.Set;

@Repository
@SuppressWarnings(value = "unchecked")
@Data

public class RedisCache<K, V> implements Cache<K, V> {
    @Autowired
    private RedisServiceImpl redisService;

    private String cacheName;

    @Override
    public V get(K k) throws CacheException {
        return (V) redisService.getHashValueByKey(cacheName, k.toString());
    }

    @Override
    public V put(K k, V v) throws CacheException {
        redisService.putHash(cacheName, k.toString(), v);
        return v;
    }

    @Override
    public V remove(K k) throws CacheException {
        V value = (V) redisService.getHashValueByKey(cacheName, k.toString());
        redisService.removeHashKey(cacheName, k.toString());
        return value;
    }

    @Override
    public void clear() throws CacheException {
        redisService.removeHash(cacheName);
    }

    @Override
    public int size() {
        return redisService.sizeHash(cacheName);
    }

    @Override
    public Set<K> keys() {
        return (Set<K>) redisService.keysHash(cacheName);
    }

    @Override
    public Collection<V> values() {
        return (Collection<V>) redisService.valuesHash(cacheName);
    }
}
